import { houseListItemAPI, houseListAreaAPI } from '@/api'
const state = {
  // 判断是否还需继续加载
  houseLength: true,
  // 房情列表
  houseList: [],
  areaList: [],
  floor: [], // 楼层
  rentType: [], // 整租合租
  price: [], // 价格
  oriented: [], // 东西南北
  roomType: [], // 房间类型，多室
  characteristic: [] // 特征,集中供暖
}
const mutations = {
  GETHOUSELIST(state, houseList) {
    // console.log(houseList.length)
    // console.log(state.houseList.length)
    // 判断是否还有租房列表
    if (state.houseList.length === houseList.length && state.houseList.length !== 20) state.houseLength = false

    state.houseList = houseList
  },
  GETAREALIST(state, areaList) {
    // state.areaList = areaList
    // console.log(areaList)
    if (state.areaList.length > 0) state.areaList = []
    areaList.area.children.forEach(item => {
      if (!item.children) {
        item.children = [{ label: '' }]
      } else {
        item.children.forEach(value => {
          if (!value.children) {
            value.children = []
          }
        })
      }
    })
    areaList.subway.children.forEach(item => {
      if (!item.children) {
        item.children = [{ label: '' }]
      } else {
        item.children.forEach(value => {
          if (!value.children) {
            value.children = []
          }
        })
      }
    })
    // 区域数据
    state.areaList.push(areaList.area, areaList.subway)
    // 整租合租
    state.floor = areaList.floor
    state.rentType = areaList.rentType
    state.price = areaList.price
    state.oriented = areaList.oriented
    state.roomType = areaList.roomType
    state.characteristic = areaList.characteristic
  }

}
const actions = {
  // 房情item列表
  async getHouseListInfo(store, data) {
    try {
      const res = await houseListItemAPI(data)
      const { list: houseList } = res.data.body
      // console.log(res)
      store.commit('GETHOUSELIST', houseList)
      return Promise.resolve(res)
    } catch (error) {
      return Promise.reject(error)
    }
  },
  // 区域选房
  async getAreaListInfo(store, data) {
    try {
      // console.log(data)
      const res = await houseListAreaAPI(data)
      const areaList = res.data.body
      // console.log(res)
      store.commit('GETAREALIST', areaList)
      return Promise.resolve(res)
    } catch (error) {
      return Promise.reject(error)
    }
  }
}
const getters = {

}

export default {
  state,
  mutations,
  actions,
  getters
}
